package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
//地址管理
public class AddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping("/address/list")
    public ResultInfo findAddress(){
        //调用service
        List<Address> list=addressService.findAddress();
        return ResultInfo.success(list);

    }
    //添加地址
    @PostMapping("/address")
    public ResultInfo addAddress(@RequestBody Address address){//接收参数
        //调用service
        addressService.addAddress(address);
        //返回结果
        return ResultInfo.success();

    }
    //修改回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findUpdate(@PathVariable("id") Long id){
        Address address=addressService.findUpdate(id);
        return ResultInfo.success(address);

    }
    //修改地址
    @PutMapping("/address")
    public ResultInfo update(@RequestBody Address address){
        //调用service
        addressService.update(address);
        //返回结果
        return ResultInfo.success();

    }

    //设置默认地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> map){
        //取出id
        Long id = map.get("id");
        //调用service
        addressService.setDefault(id);
        //返回
        return ResultInfo.success();



    }
    //查询默认地址
    @GetMapping("/address/default")
    public ResultInfo find(){
        //调用service
        Address address=addressService.findDefault();
        return ResultInfo.success(address);
    }

}
